package com.yf.service.impl;

import com.yf.dao.SignInDao;
import com.yf.dao.UserDao;
import com.yf.entity.IntegralEntity;
import com.yf.entity.SignInEntity;
import com.yf.entity.UserEntity;
import com.yf.response.RRException;
import com.yf.service.IntegralService;
import com.yf.service.SignInService;
import com.yf.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 签到Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Tue Dec 11 17:13:54 CST 2018
 */
@Service
public class SignInServiceImpl extends BaseServiceImpl<SignInEntity> implements SignInService {
    @Autowired
    private SignInDao signInDao;

    @Autowired
    private IntegralService integralService;

    @Autowired
    private UserDao userDao;

    /**内部方法**/


    /**外部方法实现**/

    /**
     * 获取签到信息
     * @param userId
     * @return
     */
    @Override
    public SignInEntity getSign(Long userId) {
        SignInEntity signInEntity = signInDao.getSign(userId);
        if(signInEntity==null){
            return new SignInEntity(userId,0);
        }else{
            if(!DateUtils.isToday(signInEntity.getLastTime())){ //今天有没有签到
                //昨天有没有签到
                Date nextDay = DateUtils.nextDay(signInEntity.getLastTime(),1);
                if(!DateUtils.isToday(nextDay)){
                    //断签的话给前端返回重置的签到信息
                    signInEntity.setContinuousDays(0);
                }
            }else {
                signInEntity.setSign(true);
            }
            return signInEntity;
        }

    }

    /**
     * 用户签到
     */
    @Override
    @Transactional
    public void signIn(Long userId) {
        UserEntity userEntity = userDao.get(userId);
        SignInEntity signInEntity = signInDao.getSign(userId);
        List<IntegralEntity> signIntegral = integralService.getAll(0); //签到积分
        List<IntegralEntity> extraIntegral = integralService.getAll(1); //签满天数赠送积分

        if(signInEntity==null){
            signInEntity = new SignInEntity(userId,0,new Date());
            userEntity.setIntegral(userEntity.getIntegral()+signIntegral.get(0).getScore()); //设置用户积分
            signInDao.save(signInEntity);
        }else {
            if(DateUtils.isToday(signInEntity.getLastTime())){ //今天有没有签到
                throw new RRException("今天已签到");
            }

            //昨天有没有签到
            Date nextDay = DateUtils.nextDay(signInEntity.getLastTime(),1);
            if(!DateUtils.isToday(nextDay)){
                //断签的话重置签到信息
                signInEntity.setContinuousDays(0);
                signInEntity.setLastTime(new Date());
                userEntity.setIntegral(userEntity.getIntegral()+signIntegral.get(0).getScore()); //设置用户积分
            }else{
                signInEntity.setContinuousDays(signInEntity.getContinuousDays()+1);
                signInEntity.setLastTime(new Date());
                if(signInEntity.getContinuousDays()<=7){  //未满7天
                    userEntity.setIntegral(userEntity.getIntegral()+signIntegral.get(signInEntity.getContinuousDays()-1).getScore()); //设置用户积分
                    if(signInEntity.getContinuousDays()==7){
                        userEntity.setIntegral(userEntity.getIntegral()+extraIntegral.get(0).getScore()); //连续签满7天赠送额外积分
                    }
                }else {  //超过7天
                    userEntity.setIntegral(userEntity.getIntegral()+signIntegral.get(6).getScore()); //设置用户积分
                }
            }
            signInDao.update(signInEntity);
            userDao.update(userEntity);
        }

    }
}
